package priv.wangy.micro.common.aop.feign;

import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.*;
import org.aspectj.lang.reflect.MethodSignature;
import org.springframework.stereotype.Component;
import priv.wangy.micro.common.utils.JsonUtil;

/**
 *@author wangy
 *@since 2024/7/12
 */
@Component
@Aspect
@Slf4j
public class FeignAspect {
    @Pointcut(value = "execution(* com.whminwei.edu.api..*.client..*(..))")
    public void feignPoint() {
    }

    @Before("feignPoint()")
    public void doBefore(JoinPoint joinPoint) {
        Object[] args = joinPoint.getArgs();
        MethodSignature methodSignature = (MethodSignature) joinPoint.getSignature();
        StringBuilder sb = new StringBuilder();
        // 可以遍历args来获取具体的参数值，或者按照你的需求来处理它们
        for (Object arg : args) {
            sb.append(arg);
        }
        log.info("请求[类：{},方法：{},入参：{}]", methodSignature.getDeclaringTypeName(), methodSignature.getMethod().getName(), sb);

    }

    @AfterReturning(value = "feignPoint()", returning = "returnValue")
    public void doAfter(JoinPoint joinPoint, Object returnValue) {
        MethodSignature methodSignature = (MethodSignature) joinPoint.getSignature();
        log.info("响应[方法：{}，出参:{}]", methodSignature.getMethod().getName(), JsonUtil.writeJson(returnValue));
    }

    @AfterThrowing(pointcut = "feignPoint()", throwing = "exception")
    public void afterThrowing(JoinPoint joinPoint, Throwable exception) {
        MethodSignature methodSignature = (MethodSignature) joinPoint.getSignature();
        log.info("-->{}处理异常,异常消息：{}", methodSignature.getMethod().getName(), exception.getMessage());
    }
}
